package com.example.JcGQS.service.impl;

import com.example.JcGQS.dao.AuditRelationshipDao;
import com.example.JcGQS.dao.HrmsDao;
import com.example.JcGQS.dao.InvoiceDao;
import com.example.JcGQS.dao.PurchaseDao;
import com.example.JcGQS.enums.PurchaseStateEnum;
import com.example.JcGQS.exception.CustomException;
import com.example.JcGQS.pojo.dto.AuditRelationshipDTO;
import com.example.JcGQS.pojo.dto.AuditRelationshipListDTO;
import com.example.JcGQS.pojo.dto.AuditUserIdDTO;
import com.example.JcGQS.pojo.dto.InsertPurchaseDTO;
import com.example.JcGQS.pojo.entity.AuditRelationship;
import com.example.JcGQS.pojo.entity.Invoice;
import com.example.JcGQS.pojo.entity.Purchase;
import com.example.JcGQS.service.AuditRelationshipService;
import com.example.JcGQS.service.PurchaseService;
import com.example.JcGQS.util.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * auditRelationshipServiceImpl
 * @author 末日
 * @date 2023/11/21.
 */
@Service
public class AuditRelationshipServiceImpl implements AuditRelationshipService {
    @Autowired
    AuditRelationshipDao auditRelationshipDao;
    @Autowired
    PurchaseDao purchaseDao;
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    InvoiceDao invoiceDao;
    @Autowired
    HrmsDao hrmsDao;

    /**
     * 根据采购id,审核人id修改审核状态
     */
    @Override
    public void auditRelationshipUpdate(AuditRelationshipDTO auditRelationshipDTO, String token) {

        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "audit_authority");
        if (integer == 0){
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        AuditRelationship auditRelationship = new AuditRelationship();
       // System.out.println(auditRelationshipDTO);
        BeanUtils.copyProperties(auditRelationshipDTO, auditRelationship);
        //System.out.println(auditRelationship);
        auditRelationshipDao.auditRelationshipUpdate(auditRelationship, userId);
        List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(auditRelationshipDTO.getPurchaseId());
        if (auditRelationshipDTO.getAuditRelationshipState().equals(PurchaseStateEnum.NOT_ALLOW.getValue())) {
            //审核失败
            purchaseDao.updatePurchaseId(auditRelationshipDTO.getPurchaseId(), PurchaseStateEnum.NOT_ALLOW.getValue());
        }
        int a = 0;
        if (auditRelationshipDTO.getAuditRelationshipState().equals(PurchaseStateEnum.ALLOWED.getValue())) {
            //System.out.println(auditRelationships);
            for (AuditRelationship relationship : auditRelationships) {
                if (relationship.getAuditRelationshipState().equals(PurchaseStateEnum.REVIEWEDING.getValue()) || relationship.getAuditRelationshipState().equals(PurchaseStateEnum.NOT_ALLOW.getValue())) {
                    a = 1;
                    break;
                }
            }
            if (a != 1) {
                purchaseDao.updatePurchaseId(auditRelationshipDTO.getPurchaseId(), PurchaseStateEnum.PURCHASING.getValue());
//                //审核成功新增发票数据
//                Invoice invoice = new Invoice();
//                invoice.setInvoiceState("无");
//                invoiceDao.InvoiceInsert(invoice);
//                Invoice invoice1 = invoiceDao.InvoiceSelect(invoice);
//                //新增的发票id赋值给采购信息
//                Purchase purchase = new Purchase();
//                purchase.setPurchaseId(auditRelationshipDTO.getPurchaseId());
//                purchase.setPurchaseInvoiceId(invoice1.getInvoiceId());
//                purchaseDao.updatePurchase(purchase);
            }
        }
    }


    /**
     * 根据采购id集合,审核人id批量修改审核状态
     */
    @Override
    public void auditRelationshipListUpdate(AuditRelationshipListDTO auditRelationshipListDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "audit_authority");
        if (integer == 0){
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        for (Integer purchaseId : auditRelationshipListDTO.getPurchaseIds()) {

        AuditRelationship auditRelationship = new AuditRelationship();
        // System.out.println(auditRelationshipDTO);
        auditRelationship.setPurchaseId(purchaseId);
        auditRelationship.setAuditRelationshipState(auditRelationshipListDTO.getAuditRelationshipState());
        //System.out.println(auditRelationship);
        auditRelationshipDao.auditRelationshipUpdate(auditRelationship, userId);
        List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseId);
        if (auditRelationshipListDTO.getAuditRelationshipState().equals(PurchaseStateEnum.NOT_ALLOW.getValue())) {
            //审核失败
            purchaseDao.updatePurchaseId(purchaseId, PurchaseStateEnum.NOT_ALLOW.getValue());
        }
        int a = 0;
        if (auditRelationshipListDTO.getAuditRelationshipState().equals(PurchaseStateEnum.ALLOWED.getValue())) {
            //System.out.println(auditRelationships);
            for (AuditRelationship relationship : auditRelationships) {
                if (relationship.getAuditRelationshipState().equals(PurchaseStateEnum.REVIEWEDING.getValue()) || relationship.getAuditRelationshipState().equals(PurchaseStateEnum.NOT_ALLOW.getValue())) {
                    a = 1;
                    break;
                }
            }
            if (a != 1) {
                purchaseDao.updatePurchaseId(purchaseId, PurchaseStateEnum.PURCHASING.getValue());
//                //审核成功新增发票数据
//                Invoice invoice = new Invoice();
//                invoice.setInvoiceState("无");
//                invoiceDao.InvoiceInsert(invoice);
//                Invoice invoice1 = invoiceDao.InvoiceSelect(invoice);
//                //新增的发票id赋值给采购信息
//                Purchase purchase = new Purchase();
//                purchase.setPurchaseId(auditRelationshipDTO.getPurchaseId());
//                purchase.setPurchaseInvoiceId(invoice1.getInvoiceId());
//                purchaseDao.updatePurchase(purchase);
            }
        }

    }
    }
}
